package EVO.service.impl;

import EVO.dao.GoodsDao;
import EVO.dao.SaleDao;
import EVO.entity.Goods;
import EVO.entity.Sale;
import EVO.excep.NotEnoughException;
import EVO.service.BuyGoodsService;

public class BuyGoodsServiceImpl implements BuyGoodsService {
    private SaleDao saleDao;
    private GoodsDao goodsDao;
    @Override
    public void buy(Integer goodsId, Integer nums) {
        System.out.println("buy方法执行........");
        Sale sale = new Sale(goodsId,nums);
        saleDao.insertSale(sale);

        Goods goods = goodsDao.selectGoods(goodsId);
        if(goods == null){
            throw new NullPointerException("编号是："+goodsId+",商品不存在");
        }else if(goods.getAmount() < nums){
            throw new NotEnoughException("编号是："+goodsId+",商品库存不足");
        }

        Goods buyGoods = new Goods();
        buyGoods.setId(goodsId);
        buyGoods.setAmount(nums);
        goodsDao.updateGoods(buyGoods);
        System.out.println("buy方法结束........");
    }


    public void setSaleDao(SaleDao saleDao) {
        this.saleDao = saleDao;
    }

    public void setGoodsDao(GoodsDao goodsDao) {
        this.goodsDao = goodsDao;
    }
}
